const removeListener = () => uni.$off('scancodedate')
const addListener = (fn) => uni.$on('scancodedate', fn)

/**
 * 扫码
 * @param fn
 * @returns
 */
const useScan = (fn) => {
  console.log('useScan start')
  if (!fn) return
  onShow(() => {
    console.log('onLoad = scan')
    addListener(fn)
  })
  onUnload(() => {
    console.log('onUnload = scan')
    removeListener()
  })
  onHide(() => {
    console.log('onHide = scan')
    removeListener()
  })
  return removeListener
}

export default useScan
